package mamikeji.liaowa.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.bean.MainBean;
import mamikeji.liaowa.ui.activity.article.Article_DetailsActivity;
import mamikeji.liaowa.widget.GlideRoundTransform;

/**
 * Created by 廉冲-android开发 on 2017/5/19.
 */
public class Main_EventViewPagerAdapter extends PagerAdapter {
    List<MainBean.HomeinfoEntity.HomearticlesEntity> homearticlesBeanList = new ArrayList<>();
    //view复用
    private LinkedList<View> mViewCache = null;

    private Context context;

    public Main_EventViewPagerAdapter(Context context,List<MainBean.HomeinfoEntity.HomearticlesEntity> homearticlesBeanList) {
        this.context = context;
        mViewCache = new LinkedList<>();
        this.homearticlesBeanList = homearticlesBeanList;
    }

    @Override
    public int getCount() {
        return homearticlesBeanList.size()*1000;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ViewHolder holder = null;
        View convertView = null;
        if (mViewCache.size() == 0) {
            convertView = View.inflate(context, R.layout.item_viewpager_main_event, null);
            holder = new ViewHolder();

            holder.textView_main_vp_aircle_type = (TextView) convertView.findViewById(R.id.textView_main_vp_aircle_type);
            holder.textView_main_vp_content = (TextView) convertView.findViewById(R.id.textView_main_vp_content);
            holder.textView_main_vp_title = (TextView) convertView.findViewById(R.id.textView_main_vp_title);
            holder.imageView_main_vp = (ImageView) convertView.findViewById(R.id.imageView_main_vp);

            convertView.setTag(holder);
        } else {
            convertView = mViewCache.removeFirst();
            holder = (ViewHolder) convertView.getTag();
        }
        container.addView(convertView);

        //对Viewpager页号求模去除View列表中要显示的项
        position %= homearticlesBeanList.size();
        if (position<0) {
            position = homearticlesBeanList.size() + position;
        }

        final MainBean.HomeinfoEntity.HomearticlesEntity homearticlesBean = homearticlesBeanList.get(position);

        holder.textView_main_vp_content.setText(homearticlesBean.getContent());
        holder.textView_main_vp_title.setText(homearticlesBean.getTitle());
        holder.textView_main_vp_aircle_type.setText(homearticlesBean.getStyle());

        holder.textView_main_vp_content.setLines(2);
        holder.textView_main_vp_content.setEllipsize(TextUtils.TruncateAt.END);

        Glide.with(context)
                .load(homearticlesBean.getImgurl())
                .transform(new CenterCrop(context)
                        ,new GlideRoundTransform(context, 5))
                .crossFade()
                .into(holder.imageView_main_vp);

        holder.imageView_main_vp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,Article_DetailsActivity.class);
                intent.putExtra("articleId",homearticlesBean.getId()+"");
                context.startActivity(intent);
            }
        });
        return convertView;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        mViewCache.add((View) object);
    }

    //View复用
    public final class ViewHolder {
        public TextView textView_main_vp_aircle_type;
        public TextView textView_main_vp_content;
        public TextView textView_main_vp_title;
        public ImageView imageView_main_vp;
    }
}
